package com.xone.xapp;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ProductCreateActivity extends BasicImageOptActivity {
	protected final String TAG = ProductCreateActivity.class.getSimpleName();
	protected Map<String, String> infoMap = new LinkedHashMap<String, String>();
	protected Spinner spinnerProductTypeName;
    protected LinearLayout outerView;
    protected String saleType;
    protected String title;
	protected List<String> productTypeList = new ArrayList<String>();
	protected boolean auth = false;
	@Override
	public String getHeaderTitle() {
		return title;
	}
	@Override
	public int getContentView() {
		if (!auth) {//没有授权
			return R.layout.assistent_info;
		}
		return R.layout.product_create;
	}
	@Override
	public Context getContext() {
		return this;
	}
	@Override
	public LinearLayout getOuterView() {
		return outerView;
	}
	@Override
	public String getImagesTag() {
		return "IMAGES_TAG";
	}
	@Override
	public String getPostUrl() {
		return "/product/createJson.html";
	}
	protected boolean checkAuth() {
		if (UserInfo.alevelUser || UserInfo.blevelUser) {
			if ("2".equals(saleType) && !UserInfo.alevelUser) {
				return false;
			}
			return true;
		}
		return true;
	}
	/**
	 * 提交完成后的回调
	 */
	@Override
	public void postFinish(JSONObject result) {
		if (null == result || result.isNull("product")) {
			Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
			return;
		}
		Toast.makeText(getContext(), "提交成功", Toast.LENGTH_SHORT).show();
		this.finish();
	}
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		if (null != bundle) {
			saleType = bundle.getString("saleType");
			if (StringUtils.isBlank(saleType)) {
				Toast.makeText(this, "无产品类型信息", Toast.LENGTH_SHORT).show();
				this.finish();
				return;
			}
		}
		if ("0".equals(saleType)) {
			title = "产品发布";
		} else if ("1".equals(saleType)) {
			title = "促销发布";
		} else if ("2".equals(saleType)) {
			title = "组团发布";
		}
		auth = checkAuth();
		super.onCreate(savedInstanceState);
		if (!auth) {//没有权限
			TextView view = (TextView)findViewById(R.id.information);
			view.setText("你所在的用户级别不能进行此项操作");
			return;
		}
		outerView = (LinearLayout)findViewById(R.id.product_create_details_zone);
		infoMap.put("productName", "产品名称不能为空");
		infoMap.put("productAddress", "产品产地不能为空");
		infoMap.put("productLocation", "产品属地不能为空");
		infoMap.put("productPrice", "产品价格不能为空");
		infoMap.put("productNum", "产品数量不能为空");
		infoMap.put("productDesc", "");
		spinnerProductTypeName = (Spinner)findViewById(R.id.productTypeName);
		productTypeList.add("冻品");
		productTypeList.add("干货");
		productTypeList.add("活鲜");
		productTypeList.add("水果");
		productTypeList.add("调料");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, productTypeList);
		spinnerProductTypeName.setAdapter(adapter);
		Button submitProduct = (Button)findViewById(R.id.submit_product);
		submitProduct.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, String> value = checkAndGetValue(infoMap);
				if (value.isEmpty()) {
					return;
				}
				int images = 0;
				View [] imageViews = new View[3];
				for (int i = 0; i < outerView.getChildCount(); i++) {
					View tv = outerView.getChildAt(i);
					String tag = (String)tv.getTag();
					if (getImagesTag().equals(tag)) {
						imageViews[images] = tv;
						images ++;
					}
				}
				if (images == 0) {
					notPass(null, "请选择至少一张图片");
					return;
				}
				showProgress();
				String productType = spinnerProductTypeName.getSelectedItem().toString();
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				for (Map.Entry<String, String> info : value.entrySet()) {
					params.add(new BasicNameValuePair("product." + info.getKey(), info.getValue()));
				}
				params.add(new BasicNameValuePair("product.saleType", saleType));
				params.add(new BasicNameValuePair("product.productType", String.valueOf(productTypeList.indexOf(productType))));
				new Base64Task(getContext(), params).execute(imageViews);
			}
		});
		Button selectImage = (Button)findViewById(R.id.button_select_image);
		selectImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int images = getImageCount();
				if (images >= 3) {
					Toast.makeText(getContext(), "最多只能选择三张图片", Toast.LENGTH_SHORT).show();
					return;
				}
				Intent intent = new Intent();
				/* 开启Pictures画面Type设定为image */  
                intent.setType("image/*");  
                /* 使用Intent.ACTION_GET_CONTENT这个Action */  
                intent.setAction(Intent.ACTION_GET_CONTENT);   
                /* 取得相片后返回本画面 */  
                startActivityForResult(intent, 1); //imageview_select_image
			}
		});
	}

}
